  <template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span >{{ '▎ ' + title }}</span>
      <span @click="showSelect = !showSelect" :style="comStyle" class="iconfont title-icon"
        >&#xe6eb;</span
      >
      <div v-if="showSelect" class="select-con">
        <div :style="comStyle" class="select-con" v-for="item in selectList" :key="item.key">
          <div class="select-item" @click="changeSelect(item.key)">
            {{ item.text }}
          </div>
        </div>
      </div>
    </div>
    <div class="com-chart" id="trend"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils'
export default {
  data () {
    return {
      chartId: null,
      allData: [],
      dataType: 'map',
      showSelect: false,
      titleFontSize: 0
    }
  },
  watch: {
    // 监听主题的变化  主题变化后更新图表
    theme () {
      this.chartId.dispose() // 重新渲染组件
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  computed: {
    ...mapState(['theme']),
    selectList () {
      if (!this.allData && !this.allData.type) {
        return []
      } else {
        return this.allData.type
      }
    },
    title () {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.dataType].title
      }
    },
    comStyle () { return { fontSize: this.titleFontSize + 'px', color: getThemeValue(this.theme).titleColor, backgroundColor: getThemeValue(this.theme).backgroundColor } }
  },
  methods: {
    changeSelect (key) {
      this.dataType = key
      this.updateChart()
      this.showSelect = false
    },
    // 初始化图表
    initChart () {
      this.chartId = this.$echarts.init(
        document.getElementById('trend'),
        this.theme
      )
      const initOption = {
        xAxis: [
          {
            type: 'category',
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        grid: {
          top: '25%',
          left: '3%',
          right: '4%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          top: '15%',
          icon: 'circle',
          left: 20 // 图例的样式
        }
      }
      this.chartId.setOption(initOption)
    },
    // 获取数据
    async getData () {
      const { data: res } = await this.$http.get('trend')
      this.allData = res
      this.updateChart()
    },
    updateChart () {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // 处理x轴数据
      const xAxisData = this.allData.common.month
      const seriesList = this.allData[this.dataType].data.map((item, index) => {
        return {
          type: 'line', // 折线图
          name: item.name,
          data: item.data,
          stack: 'all',
          areaStyle: {
            // 添加渐变效果
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: colorArr1[index] },
              { offset: 1, color: colorArr2[index] }
            ])
          }
        }
      })
      const legendArr = this.allData[this.dataType].data.map(
        (item) => item.name
      )
      const updateOption = {
        xAxis: [
          {
            data: xAxisData
          }
        ],
        series: seriesList,
        legend: {
          data: legendArr
        }
      }
      this.chartId.setOption(updateOption)
    },
    screenAdapter () {
      this.titleFontSize = document.getElementById('trend').offsetWidth / 100 * 3.6
      const adapterOptions = {
        legend: { itemWidth: this.titleFontSize, itemHeight: this.titleFontSize, itemGap: this.titleFontSize, textStyle: { fontSize: this.titleFontSize / 2 } }
      }
      this.chartId.setOption(adapterOptions)
      this.chartId.resize()
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    this.screenAdapter()
    window.addEventListener('resize', this.screenAdapter)
  },
  // 在销毁生命周期中 销毁这个方法
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
  }
}
</script>

<style lang='less' scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-item {
    cursor: pointer;
  }
  .select-con {
    background-color: #222733;
  }
}
</style>
